import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import _Slide from "@mui/material/Slide";
import React, { useEffect, useMemo, useState } from "react";
import SecondaryLayout from "../../compoents/secondary-layout";
import classes from "./accountBind.module.less";
import ScrollView from "../../compoents/scroll-view";
import { useMount, useSetState, useCreation, useLockFn } from "ahooks";
import { useSelector } from "react-redux";
import { matchTheme } from "../../utils";
import PhoneEmailBind from "./phone-email-bind";
import useServices from "../../hooks/useRequest";
import googleClient from "../../authServices/google";
import appleClient from "../../authServices/apple";
import twitterClient from "../../authServices/twitter";
import authConfig from "../../authServices/config";
import { googleBind, appleBind, twitterV1Bind } from "../../api/auth";
import { getBind } from "../../api/user";
import { useScriptLoad } from "../../hooks";
import toast from "../../compoents/toast";
import { useTranslation } from "react-i18next";
import { LocalKeyMap } from "../../local";
import { isDomesticVersion } from "../../config";
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
function AccountBind() {
    var theme = useSelector(function(st) {
        return {
            theme: st.app.theme
        };
    }).theme;
    var catchLoad = useScriptLoad([
        googleClient,
        appleClient
    ]).catchLoad;
    var _useState = _sliced_to_array(useState([]), 2), bindList = _useState[0], setBindList = _useState[1];
    var t = useTranslation().t;
    var _useSetState = _sliced_to_array(useSetState({
        enable: false
    }), 2), enable = _useSetState[0], setEnable = _useSetState[1];
    var lockFn = useLockFn(function() {
        var _ref = _async_to_generator(function(asyncFn) {
            var res;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        return [
                            4,
                            asyncFn()
                        ];
                    case 1:
                        res = _state.sent();
                        return [
                            2,
                            res
                        ];
                }
            });
        });
        return function(asyncFn) {
            return _ref.apply(this, arguments);
        };
    }());
    var services = useServices({
        googleBind: googleBind,
        appleBind: appleBind,
        twitterV1Bind: twitterV1Bind,
        getBind: getBind,
        getTwitterV1AuthCode: twitterClient.getOauth1Code
    });
    var allowLoginMethods = useCreation(function() {
        return [
            {
                coinId: "apple",
                coinName: "Apple ID",
                logo: "apple",
                handler: lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    return _ts_generator(this, function(_state) {
                        return [
                            2
                        ];
                    });
                }))
            },
            {
                coinId: "google",
                coinName: "Google",
                logo: "google"
            },
            {
                coinId: "phone",
                coinName: "Phone",
                logo: "phone"
            },
            {
                coinId: "mail",
                coinName: "Email",
                logo: "email"
            },
            {
                coinId: "twitter",
                coinName: "Twitter",
                logo: "twitter"
            }
        ].filter(function(type) {
            if (isDomesticVersion) {
                console.log(isDomesticVersion);
                return ![
                    "google",
                    "twitter"
                ].includes(type.coinId);
            }
            return true;
        });
    }, []);
    var _useSetState1 = _sliced_to_array(useSetState({}), 2), bindStatus = _useSetState1[0], setBindStatus = _useSetState1[1];
    var accountLogo = useMemo(function() {
        return {
            twitter: matchTheme(theme, {
                light: require("../../assets/icons/light/twitter.svg"),
                dark: require("../../assets/icons/dark/twitter.svg")
            }),
            // twitterv1: matchTheme(theme, {
            //   light: require('@/assets/icons/light/twitter.svg'),
            //   dark: require('@/assets/icons/dark/twitter.svg'),
            // }),
            facebook: matchTheme(theme, {
                light: require("../../assets/icons/light/facebook.svg"),
                dark: require("../../assets/icons/dark/facebookSolid.svg")
            }),
            instagram: matchTheme(theme, {
                light: require("../../assets/icons/light/ins.svg"),
                dark: require("../../assets/icons/dark/insSolid.svg")
            }),
            apple: matchTheme(theme, {
                light: require("../../assets/icons/light/apple.svg"),
                dark: require("../../assets/icons/dark/apple.svg")
            }),
            google: matchTheme(theme, {
                light: require("../../assets/icons/light/google.svg"),
                dark: require("../../assets/icons/dark/google.svg")
            }),
            phone: matchTheme(theme, {
                light: require("../../assets/icons/light/phoneSolid.svg"),
                dark: require("../../assets/icons/dark/phoneSolid.svg")
            }),
            mail: matchTheme(theme, {
                light: require("../../assets/icons/light/mailSolid.svg"),
                dark: require("../../assets/icons/dark/mailSolid.svg")
            })
        };
    }, [
        theme
    ]);
    var handSuccess = function(type, res) {
        toast.createToast({
            message: t(LocalKeyMap.successful)
        });
        setBindStatus(_define_property({}, type, true));
    };
    // twitter 取name google 和 apple取email
    var subNameMap = {
        mail: "mailStr",
        phone: "phoneStr",
        twitter: "userTwitter",
        google: "userGoogle",
        apple: "userApple"
    };
    var map = {
        phone: {
            bindHandler: function() {
                return lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    return _ts_generator(this, function(_state) {
                        setEnable({
                            type: "phone",
                            enable: true
                        });
                        return [
                            2
                        ];
                    });
                }));
            },
            unBindHandler: function() {}
        },
        mail: {
            bindHandler: function() {
                return lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    return _ts_generator(this, function(_state) {
                        setEnable({
                            type: "mail",
                            enable: true
                        });
                        return [
                            2
                        ];
                    });
                }));
            },
            unBindHandler: function() {}
        },
        twitter: {
            bindHandler: function() {
                return lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    var res, bindRes, err;
                    return _ts_generator(this, function(_state) {
                        switch(_state.label){
                            case 0:
                                _state.trys.push([
                                    0,
                                    3,
                                    ,
                                    4
                                ]);
                                return [
                                    4,
                                    services.getTwitterV1AuthCode()
                                ];
                            case 1:
                                res = _state.sent();
                                return [
                                    4,
                                    services.twitterV1Bind({
                                        onaAppId: authConfig.ONA_APPID,
                                        onaAppSecret: authConfig.ONA_APP_SECRET,
                                        token: res.oauth_token,
                                        verifier: res.oauth_verifier
                                    })
                                ];
                            case 2:
                                bindRes = _state.sent();
                                if (bindRes.code == 200) {
                                    handSuccess("twitter", bindRes.data);
                                    return [
                                        2
                                    ];
                                }
                                throw bindRes;
                            case 3:
                                err = _state.sent();
                                toast.createToast({
                                    message: err.msg || err.message,
                                    type: "errToast"
                                });
                                return [
                                    3,
                                    4
                                ];
                            case 4:
                                return [
                                    2
                                ];
                        }
                    });
                }));
            },
            unBindHandler: function() {}
        },
        google: {
            bindHandler: function() {
                return lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    var res, bindRes, err;
                    return _ts_generator(this, function(_state) {
                        switch(_state.label){
                            case 0:
                                _state.trys.push([
                                    0,
                                    4,
                                    ,
                                    5
                                ]);
                                catchLoad();
                                return [
                                    4,
                                    googleClient.init()
                                ];
                            case 1:
                                _state.sent();
                                return [
                                    4,
                                    googleClient.getCode()
                                ];
                            case 2:
                                res = _state.sent();
                                return [
                                    4,
                                    services.googleBind({
                                        clientId: authConfig.GOOGLE_CLIENTID,
                                        clientSecret: authConfig.GOOGLE_SECRET,
                                        code: res.code,
                                        redirectUri: authConfig.REDIRECT_URI,
                                        onaAppId: authConfig.ONA_APPID,
                                        onaAppSecret: authConfig.ONA_APP_SECRET
                                    })
                                ];
                            case 3:
                                bindRes = _state.sent();
                                if (bindRes.code == 200) {
                                    handSuccess("google", bindRes.data);
                                    return [
                                        2
                                    ];
                                }
                                throw bindRes;
                            case 4:
                                err = _state.sent();
                                toast.createToast({
                                    message: err.msg || err.message,
                                    type: "errToast"
                                });
                                return [
                                    3,
                                    5
                                ];
                            case 5:
                                return [
                                    2
                                ];
                        }
                    });
                }));
            },
            unBindHandler: function() {}
        },
        apple: {
            bindHandler: function() {
                return lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    var res, bindRes, err;
                    return _ts_generator(this, function(_state) {
                        switch(_state.label){
                            case 0:
                                _state.trys.push([
                                    0,
                                    4,
                                    ,
                                    5
                                ]);
                                catchLoad();
                                return [
                                    4,
                                    appleClient.init()
                                ];
                            case 1:
                                _state.sent();
                                return [
                                    4,
                                    appleClient.getCode()
                                ];
                            case 2:
                                res = _state.sent();
                                return [
                                    4,
                                    services.appleBind({
                                        onaAppId: authConfig.ONA_APPID,
                                        onaAppSecret: authConfig.ONA_APP_SECRET,
                                        identityToken: res.id_token
                                    })
                                ];
                            case 3:
                                bindRes = _state.sent();
                                if (bindRes.code == 200) {
                                    handSuccess("apple", bindRes.data);
                                    return [
                                        2
                                    ];
                                }
                                throw bindRes;
                            case 4:
                                err = _state.sent();
                                toast.createToast({
                                    message: err.msg || err.message,
                                    type: "errToast"
                                });
                                return [
                                    3,
                                    5
                                ];
                            case 5:
                                return [
                                    2
                                ];
                        }
                    });
                }));
            },
            unBindHandler: function() {}
        }
    };
    useEffect(function() {
        var originMethods = Object.keys(bindStatus);
        var tmp = allowLoginMethods.filter(function(method) {
            return originMethods.includes(method.coinId);
        });
        var _bindList = tmp.map(function(method) {
            var id = method.coinId || "mail";
            return {
                id: method.coinId,
                name: method.coinName,
                isBind: bindStatus[method.coinId],
                subNameObj: bindStatus[subNameMap[id]],
                subName: bindStatus[subNameMap[id]] || ""
            };
        });
        setBindList(_bindList);
    }, [
        JSON.stringify(bindStatus),
        JSON.stringify(allowLoginMethods)
    ]);
    var getAccountBind = function() {
        var _ref = _async_to_generator(function() {
            return _ts_generator(this, function(_state) {
                services.getBind().then(function(res) {
                    if (res.code == 200) {
                        setBindStatus(res.data);
                        return;
                    }
                    throw res;
                }).catch();
                return [
                    2
                ];
            });
        });
        return function getAccountBind() {
            return _ref.apply(this, arguments);
        };
    }();
    useMount(getAccountBind);
    // console.log(enable);
    return /*#__PURE__*/ _jsxs(_Fragment, {
        children: [
            /*#__PURE__*/ _jsx(SecondaryLayout, {
                title: t(LocalKeyMap.accountBinding),
                className: "".concat(classes.account_bind, " ").concat(enable.enable ? classes.v_h : ""),
                children: /*#__PURE__*/ _jsx("div", {
                    className: classes.wrap,
                    children: /*#__PURE__*/ _jsx(ScrollView, {
                        scrollY: true,
                        children: /*#__PURE__*/ _jsx("div", {
                            children: bindList.map(function(item, index) {
                                var _item_subNameObj, _item_subNameObj1;
                                /*#__PURE__*/ return _jsxs("div", {
                                    className: classes.account,
                                    children: [
                                        /*#__PURE__*/ _jsxs("div", {
                                            className: classes.info,
                                            children: [
                                                /*#__PURE__*/ _jsx("div", {
                                                    className: classes["logo_".concat(theme)],
                                                    children: /*#__PURE__*/ _jsx("img", {
                                                        className: classes.img,
                                                        src: accountLogo[item.id]
                                                    })
                                                }),
                                                /*#__PURE__*/ _jsxs("div", {
                                                    className: classes.nameBox,
                                                    children: [
                                                        /*#__PURE__*/ _jsx("div", {
                                                            className: classes.name,
                                                            children: item.name
                                                        }),
                                                        /*#__PURE__*/ _jsxs("div", {
                                                            className: classes.subName,
                                                            children: [
                                                                item.id === "twitter" && /*#__PURE__*/ _jsx(_Fragment, {
                                                                    children: item === null || item === void 0 ? void 0 : (_item_subNameObj = item.subNameObj) === null || _item_subNameObj === void 0 ? void 0 : _item_subNameObj.name
                                                                }),
                                                                (item.id === "google" || item.id === "apple") && /*#__PURE__*/ _jsx(_Fragment, {
                                                                    children: item === null || item === void 0 ? void 0 : (_item_subNameObj1 = item.subNameObj) === null || _item_subNameObj1 === void 0 ? void 0 : _item_subNameObj1.email
                                                                }),
                                                                item.id === "phone" || item.id === "mail" ? /*#__PURE__*/ _jsx(_Fragment, {
                                                                    children: item.subName
                                                                }) : ""
                                                            ]
                                                        })
                                                    ]
                                                })
                                            ]
                                        }),
                                        /*#__PURE__*/ _jsx("div", {
                                            className: classes.switch,
                                            children: item.isBind ? /*#__PURE__*/ _jsx("img", {
                                                className: classes.bind,
                                                src: require("../../assets/icons/binded.svg")
                                            }) : /*#__PURE__*/ _jsx("img", {
                                                onClick: function() {
                                                    return map[item.id].bindHandler();
                                                },
                                                src: require("../../assets/icons/notBinded.svg"),
                                                className: classes.bind
                                            })
                                        })
                                    ]
                                }, item.id);
                            })
                        })
                    })
                })
            }),
            /*#__PURE__*/ _jsx(_Slide, {
                in: enable.enable,
                direction: "left",
                unmountOnExit: true,
                children: /*#__PURE__*/ _jsx(SecondaryLayout, {
                    className: classes.bind_detail_layout,
                    title: t(LocalKeyMap.accountBinding),
                    backFn: function() {
                        setEnable({
                            type: undefined,
                            enable: false,
                            index: undefined
                        });
                    },
                    children: /*#__PURE__*/ _jsx(PhoneEmailBind, {
                        type: enable.type,
                        onBind: function() {
                            if (enable.enable && enable.type) {
                                setBindStatus(_define_property({}, enable.type, true));
                                setEnable({
                                    type: undefined,
                                    enable: false,
                                    index: undefined
                                });
                            }
                        }
                    })
                })
            })
        ]
    });
}
export default AccountBind;

 //# sourceMappingURL=index.js.map